package com.base.interview.devtest.api;

import com.base.interview.devtest.model.ResponseModel;
import com.base.interview.devtest.model.dto.UserDTO;
import com.base.interview.devtest.model.dto.UserLoginDTO;
import com.base.interview.devtest.model.dto.UserLogoutDTO;
import com.base.interview.devtest.model.vo.UserLoginVO;
import com.base.interview.devtest.model.vo.UserVO;
import com.base.interview.devtest.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class UserController implements IUserApi {
    @Autowired
    private IUserService iUserService;

    @Override
    public ResponseEntity<ResponseModel> status() {
        return ResponseEntity.ok(ResponseModel.success("normal"));
    }

    @Override
    public ResponseEntity<ResponseModel> register(@RequestBody UserDTO userDTO) {
        UserVO registeredUser = iUserService.register(userDTO);
        return ResponseEntity.ok(ResponseModel.success(registeredUser));
    }

    @Override
    public ResponseEntity<ResponseModel> login(@RequestBody UserLoginDTO userLoginDTO) {
        UserLoginVO login = iUserService.login(userLoginDTO);
        return ResponseEntity.ok(ResponseModel.success(login));
    }

    @Override
    public ResponseEntity<ResponseModel> logout(@RequestBody UserLogoutDTO userLogoutDTO) {
        iUserService.logout(userLogoutDTO);
        return ResponseEntity.ok(ResponseModel.success("logout success"));
    }

    @Override
    public ResponseEntity<ResponseModel> follow(@PathVariable(name = "userId") Long userId) {
        iUserService.follow(userId);
        return ResponseEntity.ok(ResponseModel.success("follow success"));
    }

    @Override
    public ResponseEntity<ResponseModel> unfollow(@PathVariable(name = "userId") Long userId) {
        iUserService.unfollow(userId);
        return ResponseEntity.ok(ResponseModel.success("unfollow success"));
    }

    @Override
    public ResponseEntity<ResponseModel> listFollowers(@PathVariable(name = "userId") Long userId) {
        List<UserVO> userVOS = iUserService.retrieveFollowers(userId);
        return ResponseEntity.ok(ResponseModel.success(userVOS));
    }

    @Override
    public ResponseEntity<ResponseModel> listFollowed(@PathVariable(name = "userId") Long userId) {
        List<UserVO> userVOS = iUserService.retrieveFollowed(userId);
        return ResponseEntity.ok(ResponseModel.success(userVOS));
    }

    @Override
    public ResponseEntity<ResponseModel> followersCount(@PathVariable(name = "userId") Long userId) {
        List<UserVO> userVOS = iUserService.retrieveFollowers(userId);
        return ResponseEntity.ok(ResponseModel.success(userVOS.size()));
    }

    @Override
    public ResponseEntity<ResponseModel> followedCount(@PathVariable(name = "userId") Long userId) {
        List<UserVO> userVOS = iUserService.retrieveFollowed(userId);
        return ResponseEntity.ok(ResponseModel.success(userVOS.size()));
    }

    @Override
    public ResponseEntity<ResponseModel> search(@PathVariable(name = "username") String username) {
        List<UserVO> userVOS = iUserService.search(username);
        return ResponseEntity.ok(ResponseModel.success(userVOS));
    }
}
